package weaver.interfaces.unine;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.io.IOException;

/**
 * @ClassName SupplierUtil
 * @Description TODO
 * @Author ocean
 * @Date 2022/9/14 11:20
 * @Version 1.0
 */
public class SupplierUtil extends BaseBean {
    OkHttpClient client = new OkHttpClient();

//    public static void main(String[] args) {
//        String  url= "http://221.6.120.194:9001/U9C/RestServices/UFIDA.U9.Cust.Yl.CustApi.BaseSV.ICreateSupplySV.svc/do";
//        Map<String,Object> inputMap = new HashMap<String,Object>();
//        JSONObject jsonData = new JSONObject(  );
//        jsonData.put("CultureName","zh-CN");
//        jsonData.put("EntCode","901");
//        jsonData.put("OrgCode","999");
//        jsonData.put("UserCode","admin");
//        inputMap.put("context", jsonData);
//        inputMap.put("orgCode","999");
//        inputMap.put("createdBy","103869");
//        inputMap.put("name","供应商15");
//        inputMap.put("contactMan","李斯");
//        inputMap.put("bankName","中国银行");
//        inputMap.put("toOrgCode","156031901");
//        inputMap.put("invType","增值税专用发票");
//        inputMap.put("contactPhone","1678934567");
//        inputMap.put("bankAccountName","345678900234");
//        inputMap.put("setMethod","承兑/现金不扣点");
//        inputMap.put("taxRate",0.05);
//        inputMap.put("stateTaxNo","4567890");
//        inputMap.put("jyAddress","河北省1");
//        inputMap.put("zcAddress","河北省2");
//        inputMap.put("contactMail","1678934567@qq.com");
//        inputMap.put("suppType","境内供应商");
//        inputMap.put("isMISC","否");
//        inputMap.put("accPeriod","60");
//        inputMap.put("confirmCode","A1");
//        inputMap.put("sarea","");
//        inputMap.put("sprovince","河北省");
//        inputMap.put("scity","石家庄市");
//        inputMap.put("scounty","长安区");
//        SupplierUtil supplierUtil = new SupplierUtil();
//        String result = supplierUtil.doSupplier(url, JSON.toJSONString(inputMap));
//
//       Map<String, Object> jsonMap= JSON.parseObject(result,HashMap.class );
//        System.out.println(jsonMap);
//
//
//        Map<String, Object> stringMap= JSON.parseObject( (String) jsonMap.get( "d" ),HashMap.class );
//
//        System.out.println("d="+result);
//    }


    /**
     * 发送数据
     * @param url
     * @param input
     * @return
     */
    public String doSupplier(String url,String input){

        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, input);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .addHeader("Content-Type", "application/json")
                .addHeader("cache-control", "no-cache")
                .addHeader("Postman-Token", "e43b6b90-8e8e-477a-961e-21b8dd25f126")
                .build();

        Response response = null;
        String back = "";
        try {
            response = client.newCall(request).execute();
            back = response.body().string();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return back;
    }

    /**
     * 获取
     * @return
     */
    public JSONObject getContextInfo(){
        String cultureName = getPropValue("u9yk","CultureName");
        String entCode = getPropValue("u9yk","EntCode");
        String orgCode = getPropValue("u9yk","OrgCode");
        String userCode = getPropValue("u9yk","UserCode");
        JSONObject jsonData = new JSONObject(  );
        jsonData.put("CultureName",cultureName);
        jsonData.put("EntCode",entCode);
        jsonData.put("OrgCode",orgCode);
        jsonData.put("UserCode",userCode);
        return  jsonData;
    }

    /**
     * 获取公司编码
     * @param comId
     * @return
     */
    public String getCompanyCode(String comId){
        RecordSet rs = new RecordSet();
        rs.execute("select subCompanyCode from HrmSubCompany where id='"+comId+"'");
        rs.next();
        String subCompanyCode = Util.null2String(rs.getString(1));
        return subCompanyCode;
    }

    /**
     * 获取部门编码
     * @param deptId
     * @return
     */
    public String getDeptCode(String deptId){
        RecordSet rs = new RecordSet();
        rs.execute("select departmentCode from HrmDepartment where id='"+deptId+"'");
        rs.next();
        String departmentCode = Util.null2String(rs.getString(1));
        return departmentCode;
    }

    /**
     * 获取人员工号
     * @param userId
     * @return
     */
    public String getWorkCode(String userId){
        RecordSet rs = new RecordSet();
        rs.execute("select workCode from hrmResource where id='"+userId+"'");
        rs.next();
        String workCode = Util.null2String(rs.getString(1));
        return workCode;
    }

    /**
     * 获取下拉框的值
     * @param billId
     * @param fieldName
     * @param selectValue
     * @return
     */
    public String getSelectValue(int billId,String fieldName,String selectValue)
    {
        String sql = "select t2.selectname from workflow_billfield t1 , workflow_SelectItem t2   " +
                "where t1.id=t2.fieldid and  t1.billid ="+billId+" and  t1.fieldname ='"+fieldName+"' and  t2.selectvalue="+selectValue;
        RecordSet rs = new RecordSet();
       writeLog("getSelectValue="+sql);
        rs.execute(sql);
        rs.next();
        String selectName = Util.null2String(rs.getString("selectname"));
        return selectName;
    }

    /**
     * 省份名称
     * @param provinceId
     * @return
     */
    public String getProvince(String provinceId){
        RecordSet rs = new RecordSet();
        rs.execute(" select provincename from HrmProvince where id='"+provinceId+"'");
        rs.next();
        String provinceName = Util.null2String(rs.getString("provincename"));
        return provinceName;
    }

    /**
     * 市名词
     * @param id
     * @return
     */
    public String getCity(String id){
        RecordSet rs = new RecordSet();
        rs.execute(" select cityname from HrmCity where id='"+id+"'");
        rs.next();
        String cityname = Util.null2String(rs.getString("cityname"));
        return cityname;
    }

    /**
     * 县
     * @param id
     * @return
     */
    public String getCityTwo(String id){
        RecordSet rs = new RecordSet();
        rs.execute(" select cityname from hrmcitytwo where id='"+id+"'");
        rs.next();
        String cityname = Util.null2String(rs.getString("cityname"));
        return cityname;
    }

    /**
     * u9供应商名称
     * @param id
     * @return
     */
    public String getu9gysbm(String id,String name){
        RecordSet rs = new RecordSet();
        rs.executeQuery(" select "+name+" from uf_gysxx where id=?",id);
        rs.next();
        return Util.null2String(rs.getString(name));
    }
}
